﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;
using Common;
using System.Configuration;

namespace DAL
{
    public class FunctionDAL : DB
    {
        #region 公共属性

        #endregion

        #region 构造函数
        public FunctionDAL(LoginInfo info)
        {
            //base.loginInfo = info;
            base.loginInfo = new LoginInfo();
        }

        #endregion

        #region 公共函数

        public FunctionInfo GetInfoById(string funID)
        {
            HIDB.dbo cmd = base.conn();
            try
            {
                string strSql = @"SELECT * FROM [dbo].[FunTable] WHERE Guid=@Guid";
                IDataParameter[] cmdParms = { new SqlParameter("@Guid", funID) };
                DataTable dts = cmd.GetDataTable(strSql, cmdParms);
                var list = dts.MapToList<FunctionInfo>();
                if (list != null && list.Count > 0)
                {
                    var info = list[0];
                    return info;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("[dbo].[FunTable]", "GetInfoById", 0, 1, ex, "", "");
            }
            return null;
        }


        public List<FunctionInfo> GetList(string funName, int funType, string actionType, out int recordCount)
        {
            recordCount = 0;
            List<FunctionInfo> result = new List<FunctionInfo>();
            try
            {
                HIDB.dbo cmd = base.conn();
                List<SqlParameter> cmdParms = new List<SqlParameter>();
                string strSql = @"SELECT f.[Guid],[FunName],[ParamCount],[FunType],f.[Description], d.[Name] as [ActionType], d.[value] as [ActionValue] FROM [dbo].[FunTable] f left join  [Working_db].[Dictionary] d on
f.ActionType=d.Value where d.ParentID=(select guid from [Working_db].[Dictionary] where Description='ActionType') ";
                if (!string.IsNullOrEmpty(funName))
                {
                    strSql += " AND f.FunName like '%'+@FunName+'%'";
                    cmdParms.Add(new SqlParameter("@FunName", funName));
                }
                if (funType > 0)
                {
                    strSql += " AND f.FunType=@FunType";
                    cmdParms.Add(new SqlParameter("@FunType", funType));
                }
                if (!string.IsNullOrEmpty(actionType))
                {
                    strSql += " AND d.Value=@ActionType";
                    cmdParms.Add(new SqlParameter("@ActionType", actionType));
                }

                DataTable dt = cmd.GetDataTable(strSql, cmdParms.ToArray());
                result = dt.MapToList<FunctionInfo>();
                recordCount = result.Count;
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<FunctionInfo> GetByNameWithOutGuid(string funName, string guid = null)
        {
            List<FunctionInfo> result = new List<FunctionInfo>();
            try
            {
                string strSql = @"SELECT [Guid]
      ,[FunName]
      ,[ParamCount]
      ,[FunType]
      ,[Description]
  FROM [dbo].[FunTable]
  WHERE 1=1";
                List<IDataParameter> cmdParms = new List<IDataParameter>();

                strSql += " AND FunName =@FunName";
                cmdParms.Add(new SqlParameter("@FunName", funName));
                if (!string.IsNullOrEmpty(guid))
                {
                    strSql += " AND Guid!=@Guid";
                    cmdParms.Add(new SqlParameter("@Guid", guid));
                }
                HIDB.dbo cmd = base.conn();
                DataTable dt = cmd.GetDataTable(strSql, cmdParms.ToArray());
                result = dt.MapToList<FunctionInfo>();
            }
            catch (Exception ex)
            {
                Logger.LogError("[dbo].[FunTable]", "GetByNameWithOutGuid", 0, 1, ex, "", "");
            }
            return result;

        }

        public int Insert(FunctionInfo functionInfo)
        {
            int num = -1;
            //创建cmd对象
            HIDB.dbo cmd = base.conn();
            //执行命令返回结果
            try
            {
                string strSql = @"INSERT INTO [dbo].[FunTable]
           ([Guid]
           ,[FunName]
           ,[ParamCount]
           ,[FunType]
           ,[Description]
           ,[ActionType])
     VALUES (@Guid
           ,@FunName
           ,@ParamCount
           ,@FunType
           ,@Description
           ,@ActionType)";
                SqlParameter[] cmdParms = new SqlParameter[]
                {
                    new SqlParameter("@Guid",functionInfo.Guid)
                     ,new SqlParameter("@FunName",functionInfo.FunName)
                     ,new SqlParameter("@ParamCount",functionInfo.ParamCount)
                     ,new SqlParameter("@FunType",functionInfo.FunType)
                     ,new SqlParameter("@Description",functionInfo.Description??string.Empty)
                     ,new SqlParameter("@ActionType",functionInfo.ActionType)
                    
                };
                num = cmd.ExecuteSql(strSql, cmdParms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return num;
        }

        public int Update(FunctionInfo functionInfo)
        {
            int num = -1;

            //创建cmd对象
            HIDB.dbo cmd = base.conn();

            //执行命令返回结果
            try
            {
                string strSql = @"UPDATE [dbo].[FunTable]
   SET [FunName] = @FunName
      ,[ParamCount] = @ParamCount
      ,[FunType] = @FunType
      ,[Description] = @Description
      ,[ActionType] = @ActionType
 WHERE [Guid] = @Guid";
                SqlParameter[] cmdParms = new SqlParameter[]
                {
                    new SqlParameter("@Guid",functionInfo.Guid)
                     ,new SqlParameter("@FunName",functionInfo.FunName)
                     ,new SqlParameter("@ParamCount",functionInfo.ParamCount)
                     ,new SqlParameter("@FunType",functionInfo.FunType)
                     ,new SqlParameter("@Description",functionInfo.Description??string.Empty)
                     ,new SqlParameter("@ActionType",functionInfo.ActionType)
                    
                };
                num = cmd.ExecuteSql(strSql, cmdParms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return num;
        }

        public int Delete(string funID)
        {
            HIDB.dbo cmd = base.conn();
            try
            {
                string strSql = @"DELETE FROM [dbo].[FunTable] WHERE [Guid]=@Guid";
                SqlParameter[] cmdParms = new SqlParameter[]
                {
                    new SqlParameter("@Guid",funID)
                };
                cmd.ExecuteSql(strSql, cmdParms);
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string GetDataType(string value)
        {
            string conn = ConfigurationManager.ConnectionStrings["ConnectionReadOnly"].ToString();
            HIDB.dbo cmd = HIDB.common.make(HIDB.DBType.SQLServer, conn); 
            try
            {
                return cmd.GetDataTable(value).Rows[0][0].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

    }
}
